package com.baseframe.chat.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baseframe.chat.entity.SnsChatUser;
import com.baseframe.chat.entity.WebsocketMessage;
import com.baseframe.chat.entity.vo.SnsChatUserVO;
import com.baseframe.chat.service.SnsChatUserService;
import com.baseframe.chat.mapper.SnsChatUserMapper;
import com.baseframe.tool.result.R;
import com.baseframe.user.entity.BaseUser;
import com.baseframe.user.feign.BaseUseFeign;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**

* @description 针对表【sns_chat_user(用户私聊表)】的数据库操作Service实现
* @createDate 2023-07-12 03:54:03
*/
@Service
@AllArgsConstructor
public class SnsChatUserServiceImpl extends ServiceImpl<SnsChatUserMapper, SnsChatUser>
    implements SnsChatUserService{

    private BaseUseFeign baseUseFeign;


    @Override
    public List<SnsChatUserVO> getUserChatRecords(Long senderUserId, Long receiverUserId) {


        return getBaseMapper().getUserChatRecords(senderUserId,receiverUserId);
    }

    @Override
    public boolean saveChatRecord(WebsocketMessage websocketMessage) {
        SnsChatUser snsChatUser = new SnsChatUser();
        R<BaseUser> senderUserR = baseUseFeign.findAllByUsername(websocketMessage.getSender());
        R<BaseUser> receiverUserR = baseUseFeign.findAllByUsername(websocketMessage.getReceiver());

        if((senderUserR.getCode()!=200||senderUserR.getData() == null)||(receiverUserR.getCode()!=200||receiverUserR.getData() == null)){
            return false;
        }
        //封装聊天记录
        snsChatUser.setSenderId(senderUserR.getData().getId());
        snsChatUser.setReceiverId(receiverUserR.getData().getId());
        snsChatUser.setSendTime(new Date());
        snsChatUser.setContent(websocketMessage.getMessage());
        snsChatUser.setCreateBy(senderUserR.getData().getId());
        snsChatUser.setUpdateBy(senderUserR.getData().getId());
        snsChatUser.setCreateTime(new Date());
        snsChatUser.setUpdateTime(new Date());

        return save(snsChatUser);
    }
}




